//
//  MLABaseTitleTableView.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/18.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLABaseTitleTableVC.h"
#import "MLABaseTableCell.h"

@interface MLABaseTitleTableVC ()

@end

@implementation MLABaseTitleTableVC

- (instancetype)init {
    self = [super init];
    if (self) {
        _tableViewStyle = UITableViewStyleGrouped;
        _lineEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:_tableViewStyle];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorColor = CX_GetColorHex(0xF0F1F4, 1);
    _tableView.rowHeight = 50.f;
    _tableView.estimatedSectionFooterHeight = 0.0f;
    _tableView.estimatedSectionHeaderHeight = 0.0f;
    _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [_tableView registerClass:[MLABaseTableCell class] forCellReuseIdentifier:kCellID];
    [self.view addSubview:_tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleView.mas_bottom);
        make.left.bottom.right.equalTo(self.view);
    }];
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titlesArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLABaseTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID forIndexPath:indexPath];
    if (!_showAccessoryView) {
        cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right_arrow_icon"]];
    }
    cell.textLabel.text = _titlesArr[indexPath.row];
    cell.textLabel.font = CX_GetNormalFont(12);
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_tableView.separatorStyle == UITableViewCellSeparatorStyleNone) {
        return;
    }
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:_lineEdgeInsets];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:_lineEdgeInsets];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)setHaveRefreshHeader:(BOOL)haveRefreshHeader {
    _haveRefreshHeader = haveRefreshHeader;
    
    if (_haveRefreshHeader) {
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    }
}

- (void)loadData {
    [_tableView.mj_header endRefreshing];
    _page = 1;
}

- (void)setHaveRefreshFooter:(BOOL)haveRefreshFooter {
    _haveRefreshFooter = haveRefreshFooter;
    
    if (_haveRefreshFooter) {
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
}

- (void)loadMoreData {
    
}


@end
